package controller;

import DataAcess.Bus;
import Interface.MainGui;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UnsupportedLookAndFeelException;


/*
 * Class ID	: SE715
 * Student ID	: 02967
 * Student Name : Vu Ngoc Trung
 * Due Date     : Jul 13, 2013
 * I declare that this assignment is my own work in accordance with FPT Policy.
 */
public class MainController implements controller.Controller {

    MainGui form;
    static ArrayList<Bus> arrbus;
    ListAllController listAllController;
    SearchByNumberController searchByNumberController;
    SearchByNameController SearchByNameController;
    SearchStartEndController searchStartEndController;
    SearchFamousController searchFamousController;

    public MainController() throws ClassNotFoundException {
        arrbus = new DataAcess.BusReader().getArrBus();
        form = new MainGui(this);
        form.setVisible(true);



       
    }

    @Override
    public void handleEvent(String event) {
        //call to ListAll controller
        if (event.equals("ListAll")) {
            form.tabChinh.setSelectedIndex(1);
            listAllController = new ListAllController(arrbus, this, form);
            listAllController.handleEvent("ListAll");
        }
        //tranform main form command that view bus are already chosen in display table
        if (event.equals("DisplayAllView")) {
            listAllController.handleEvent("View");
        }
        //call to search number controller
        if (event.equals("Search Number")) {
            form.tabChinh.setSelectedIndex(2);
            searchByNumberController = new SearchByNumberController(arrbus, this, form);
        }
        //start search number search number action
        if (event.equals("Search No bt clicked")) {
            searchByNumberController.handleEvent("Search Number");
        }
        //call to search name controller
        if (event.equals("Search Name")) {
            form.tabChinh.setSelectedIndex(3);
            SearchByNameController = new SearchByNameController(arrbus, this, form);
            ShowAll();
        }
        //Start search name action
        if (event.equals("Search Name bt press")) {
            SearchByNameController.handleEvent("Search Name");
        }
        //View bus are already chosen
        if (event.equals("SearchNameView")) {
            SearchByNameController.ViewBus();
        }
        //call to search by start end point funtion
        if (event.equals("SearchStartEnd")) {
            form.tabChinh.setSelectedIndex(4);
            searchStartEndController = new SearchStartEndController(arrbus, this, form);
        }
        //Start search startEnd action
        if (event.equals("SearchStartEnd bt clicked")) {
            searchStartEndController.handleEvent("SearchStartEnd");
        }
        //view bus are already chosen in search start end table
        if (event.equals("SearchStarEndView")) {
            searchStartEndController.Viewbus();
        }
        //call to search famous funtion
        if (event.equals("Search Famous")) {
            form.tabChinh.setSelectedIndex(5);
            searchFamousController = new SearchFamousController(arrbus, this, form);
            form.cbtypeSuggest.setSelectedIndex(2);
        }
        // call to show all famous location are in type already chosen
        if (event.equals("cbtypeSuggest press")) {
            searchFamousController.handleEvent("show all of type");
        }
        // show all bus passing the location are already chosen
        if (event.equals("show suggestions")) {
            searchFamousController.handleEvent("show suggestions");
        }
        //view bus that are already chosen in suggestion table
        if (event.equals("SuggestTionView")) {
            searchFamousController.handleEvent("ViewBus");
        }
        if (event.equals("ShowAll")) {
            ShowAll();
        }
    }

    public void ShowAll() {

        int i = 0;
        for (Bus bus : arrbus) {
            form.tbStreet.setValueAt(bus.getNumber(), i, 0);
            form.tbStreet.setValueAt(bus.getName(), i, 1);
            i++;
        }
    }

    public static void main(String[] args) throws ClassNotFoundException {
        MainController mainController = new MainController();
    }
}
